<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */
 
 namespace HiCommon\Repository;

use HiCommon\Model\EntrustPermissions;
use HiCommon\Model\EntrustRoleUser;
use Illuminate\Support\Facades\Cache;

class EntrustPermissionsRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\EntrustPermissions();
    }

    /**
     * 根据where条件获取正常的权限数据并排序
     *
     * @param array $arr_where
     *
     * @return \Illuminate\Support\Collection
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function normal_all_where_sort($arr_where = []){
        $arr_permission = self::model_child()->where($arr_where)->get()->toarray();
        $result = collect($arr_permission)->sortBy('id')->sortBy('sort')->values();
        return $result;
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return EntrustPermissions::commonget($arr_option);
    }

    /**
     * 通过where获取所有数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_by_where($arr_where,$arr_option = [])
    {
        return EntrustPermissions::where($arr_where)->commonget($arr_option);

    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [])
    {
        return EntrustPermissions::commonpage($arr_option);
    }
    
    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return EntrustPermissions::where($arr_where)->commonpage($arr_option);

    }
    
    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return EntrustPermissions::where($arr_where)->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return EntrustPermissions::where($arr_where)->sum($s_field);
    }

//    /**
//     * 添加一条数据
//     *
//     * @param array $arr_input
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function add_one($arr_input)
//    {
//        return EntrustPermissions::create($arr_input);
//    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return EntrustPermissions::commonfind($n_id, $arr_option);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return EntrustPermissions::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return EntrustPermissions::where('id', $n_id)
                             ->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return EntrustPermissions::where('id',$n_id)->update(['state'=>$n_state]);

    }
    
    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return EntrustPermissions::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return EntrustPermissions::where('id',$n_id)->decrement($s_field,$n_step);
    }


    /**
     * 通过用户id获取用户权限
     *
     * @param int $n_uid
     *
     * @return \HiCommon\Model\EntrustRoleUser[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_permissions_by_uid(int $n_uid){
        return EntrustRoleUser::with('role.role_permission')
                              ->where('user_id',$n_uid)
                              ->get();
    }

    /**
     * 通过ids获取菜单列表
     *
     * @param $arr_ids
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_menu_by_permission_ids($arr_ids){
        return EntrustPermissions::orderBy('sort', 'ASC')
                                 ->orderBy('id', 'ASC')
                                 ->whereIn('id',$arr_ids)
                                 ->where(['status' => 1, 'is_menu' => 1])
                                 ->select(['id','route','alias','name','ico','pid'])
                                 ->get();
    }

    /**
     * 通过ids获取权限列表
     *
     * @param $arr_ids
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_permissions_by_ids($arr_ids){
        return EntrustPermissions::orderBy('sort', 'ASC')
                                 ->orderBy('id', 'ASC')
                                 ->whereIn('id',$arr_ids)
                                 ->where(['status' => 1])
                                 ->select(['id','route','alias','name','ico','pid'])
                                 ->get();
    }


}